﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SharePointExplorer.SharePoint;
using SharePointExplorer.Classes;
using FluidKit.Controls;
using SharePointExplorer.KinectControls;

namespace SharePointExplorer.UserControls
{
    /// <summary>
    /// Interaction logic for DashboardSubItems.xaml
    /// </summary>
    public partial class DashboardSubItems : UserControl
    {
        NavigationManager nm = new NavigationManager();
        App app = (App)Application.Current;
        SharePointExplorer.Classes.ListBoxHandler listHandler = null;
        int totalPageCount;
        int pageIndex = 1;
        public DashboardSubItems()
        {
            InitializeComponent();
            
        }

        void DashboardSubItems_Loaded(object sender, RoutedEventArgs e)
        {
            
            
        }

        public DashboardSubItems(string ListName) 
        {
            InitializeComponent();
            app.MainWindowInstance.txtCurrentScreen.Text = ListName;
            KinectHelper.TogglePostureDetectedHandler(true);
            app.MainWindowInstance.DeactivateButton.Visibility = Visibility.Visible;
            app.MainWindowInstance.HomeButton.Visibility = Visibility.Visible;
            app.MainWindowInstance.backButton.Visibility = Visibility.Visible;

            this.Loaded += new RoutedEventHandler(DashboardSubItems_Loaded);
            SharePointUtils.FromList = ListName;
            
            listHandler = new ListBoxHandler(SharePointHelper.GetListItemCollection(ListName));
            totalPageCount = listHandler.pageCount;
            if (listHandler.pageCount > 1)
            {
                btnNext.Visibility = Visibility.Visible;
                btnPrevious.Visibility = Visibility.Collapsed;
            }
            else
            {
                btnNext.Visibility = Visibility.Collapsed;
                btnPrevious.Visibility = Visibility.Collapsed;
            }

            DashboardSubDetailsList.ItemsSource = listHandler.GetFirstPageCollection();

            
            //var list = SharePointHelper.GetListCollection();
            //DashboardDetailsList.ItemsSource = list;
        }

        private void KinectButton_Click(object sender, RoutedEventArgs e)
        {
            SharePointHelper.GetFile(sender);
            if (SharePointUtils.MoveFlag || SharePointUtils.CopyFlag)
            {
                nm.NavigateContent(this, new DashboardItem("List"),Direction.LeftToRight);
            }
            else if (SharePointUtils.DeleteFlag)
            {
                SharePointHelper.Delete();

                DashboardSubDetailsList.ItemsSource = null;
                DashboardSubDetailsList.ItemsSource = SharePointHelper.GetListItemCollection(SharePointUtils.FromList);
            }
            else
            {
                string extension = System.IO.Path.GetExtension(SharePointUtils.FileName);
                if (extension.ToLower() == ".txt")
                {
                    nm.NavigateContent(this, new TextReader(), Direction.RightToLeft);
                }
                else if (extension.ToLower() == ".jpg" || extension.ToLower() == ".png")
                {
                    nm.NavigateContent(this, new ImageViewer(SharePointUtils.FileName), Direction.RightToLeft);
                }
                app.MainWindowInstance.txtCurrentScreen.Text = SharePointUtils.FileName;
            }
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            pageIndex++;
            btnPrevious.Visibility = Visibility.Visible;
            DashboardSubDetailsList.ItemsSource = null;
            DashboardSubDetailsList.ItemsSource = listHandler.GetNextPageCollection();
            if (pageIndex == totalPageCount)
            {
                btnNext.Visibility = Visibility.Collapsed;
            }

            
        }

        private void btnPrevious_Click(object sender, RoutedEventArgs e)
        {
            pageIndex--;
            DashboardSubDetailsList.ItemsSource = null;
            DashboardSubDetailsList.ItemsSource = listHandler.GetPreviousPageCollection();

            if (pageIndex == 1)
            {
                btnPrevious.Visibility = Visibility.Collapsed;
            }

            btnNext.Visibility = Visibility.Visible;
        }
    }
}
